package com.ning.common.web.response;


import lombok.Data;

import java.io.Serializable;

/**
 * 统一Controller中RESTFul风格接口返回的结果
 */
@Data
public class Result<T> implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private Integer code;
	private String msg;
	private T data;
	
	private Result() {}
	
	private Result(Integer code, String msg) {
		this.code = code;
		this.msg = msg;
	}
	
	private void setResultCode(ResultCode code) {
		// 左边是result的code属性
		// 右边是枚举对象的code()方法
		this.code = code.code();
		this.msg = code.message();
	}
	
	/**
	 * 操作失败，自定义code和msg
	 */
	public static Result failure(Integer code, String msg) {
		Result result = new Result(code,msg);
		return result;
	}
	
	/**
	 * 操作成功，没有返回的数据
	 */
	public static Result success() {
		Result result = new Result();
		result.setResultCode(ResultCode.SUCCESS); // 1 s
		return result;
	}
	
	/**
	 * 操作成功，有返回的数据
	 */
	public static <T> Result<T> success(T data) {
		Result result = new Result();
		// ResultCode.SUCCESS 枚举对象
		result.setResultCode(ResultCode.SUCCESS);
		result.setData(data);
		return result;
	}
	/**
	 * 操作成功，有返回的数据
	 */
	public static <T> Result<T> success(String message) {
		return new Result(ResultCode.SUCCESS.code(),message);
	}
	
	/**
	 * 操作失败，没有返回的数据
	 */
	public static Result failure(ResultCode resultCode) {
		Result result = new Result();
		result.setResultCode(resultCode);
		return result;
	}

	/**
	 * 操作失败，没有返回的数据
	 */
	public static Result failure(String message) {
		return new Result(ResultCode.ERROR.code(),message);
	}
	
	/**
	 * 操作失败，有返回的数据
	 */
	public static <T> Result<T> failure(ResultCode resultCode, T data) {
		Result result = new Result();
		result.setResultCode(resultCode);
		result.setData(data);
		return result;
	}

}
